package com.cloudlead.common.lang.api;

/**
 * 响应状态码
 * 
 * @author dean.lu
 * 
 */
public enum ResponseCode {
	OK(20000, "OK"),

	CREATED(20100, "Created"),

	ACCEPTED(20200, "Accepted"),

	NO_CONTENT(20400, "No Content"),

	BAD_REQUEST(40000, "Bad Request"),

	SC_UNAUTHORIZED(40100, "Unauthorized Access"),

	FORBIDDEN(40300, "Forbidden"),

	RESOURCE_NOT_FOUND(40400, "Resource Not Found"),

	RESOURCE_NOT_ACCEPTABLE(40600, "Resource Not Acceptable"),

	REQUEST_TIMEOUT(40800, "Request Timeout"),

	INTERNAL_SERVER_ERROR(50000, "Internal Server Error"),

	SERVICE_UNAVAILABLE(50300, "Service Unavailable"),

	DATA_ACCESS_ERROR(51000, "Data Access Error"),

	NOT_SUPPORTED_ERROR(51200, "Not supported Error");

	private final int code;
	private final String reason;

	private ResponseCode(int code, String reason) {
		this.code = code;
		this.reason = reason;
	}

	public int code() {
		return code;
	}

	public String reason() {
		return reason;
	}

	/**
	 * Convert a numerical status code into the corresponding Status
	 * 
	 * @param statusCode
	 *            the numerical status code
	 * @return the matching Status or null is no matching Status is defined
	 */
	public static ResponseCode fromStatusCode(final int statusCode) {
		for (ResponseCode s : ResponseCode.values()) {
			if (s.code == statusCode) {
				return s;
			}
		}
		return null;
	}

}
